package me.riddhimanadib.fastformbuilder;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;
import android.util.Pair;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.riddhimanadib.formmaster.helper.FormBuildHelper;
import me.riddhimanadib.formmaster.listener.OnFormElementValueChangedListener;
import me.riddhimanadib.formmaster.model.FormElement;
import me.riddhimanadib.formmaster.model.FormHeader;
import me.riddhimanadib.formmaster.model.FormObject;

public class FullscreenFormActivity extends AppCompatActivity {

    private static final String TAG = "FullscreenFormActivity";

    private RecyclerView mRecyclerView;
    private FormBuildHelper mFormBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen_form);

        setupToolBar();

        setupForm();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setupToolBar() {

        final ActionBar actionBar = getSupportActionBar();

        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        }

    }

    private void setupForm() {

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mFormBuilder = new FormBuildHelper(this, mRecyclerView, valueChangedListener);

        FormHeader header1 = FormHeader.createInstance().setTitle("Personal Info");
        FormElement element11 = FormElement.createInstance()
                                           .setType(FormElement.TYPE_EDITTEXT_EMAIL)
                                           .setTitle("Email")
                                           .setRequired(true)
                                           .setHint("Enter Email");
        FormElement element12 = FormElement.createInstance()
                                           .setType(FormElement.TYPE_EDITTEXT_PHONE)
                                           .setTitle("Phone")
                                           .setRequired(true)
                                           .setValue("+8801712345678");
        FormElement element13 = FormElement.createInstance()
                                           .setType(FormElement.TYPE_SWITCH)
                                           .setTitle("Married")
                                           .setValue("No")
                                           .setCheckedAndNotCheckValue(new Pair<>("Yes", "No"));
        List<String> genders = new ArrayList<>();
        genders.add("Male");
        genders.add("Female");
        FormElement element14 = FormElement.createInstance().setType(FormElement.TYPE_RADIOGROUP).setTitle("Gender").setOptions(genders);
        FormHeader header2 = FormHeader.createInstance().setTitle("Family Info");
        FormElement element21 = FormElement.createInstance()
                                           .setType(FormElement.TYPE_EDITTEXT_TEXT_SINGLELINE)
                                           .setTitle("Location")
                                           .setValue("Dhaka");
        FormElement element22 = FormElement.createInstance()
                                           .setTag(99)
                                           .setType(FormElement.TYPE_EDITTEXT_ASSISTANT_BUTTON)
                                           .setFormAssistantCallback(new FormElement.FormAssistantCallback() {
                                               @Override
                                               public void onAssistantButtonClick(EditText valueEditText) {
                                                   showInputDialog(mFormBuilder.getFormElement(99).getValue(), valueEditText);
                                               }
                                           })
                                           .setTitle("Address");
        FormElement element23 = FormElement.createInstance().setType(FormElement.TYPE_EDITTEXT_NUMBER).setTitle("Zip Code").setValue("1000");

        FormHeader header3 = FormHeader.createInstance().setTitle("Schedule");
        FormElement element31 = FormElement.createInstance().setType(FormElement.TYPE_PICKER_DATE).setTitle("Date");
        FormElement element32 = FormElement.createInstance().setType(FormElement.TYPE_PICKER_TIME).setTitle("Time");
        FormElement element33 = FormElement.createInstance().setType(FormElement.TYPE_PICKER_DATE_AND_TIME).setTitle("DateTime");
        FormElement element34 = FormElement.createInstance().setType(FormElement.TYPE_EDITTEXT_PASSWORD).setTitle("Password").setValue("abcd1234");

        FormHeader header4 = FormHeader.createInstance().setTitle("Preferred Items");
        List<String> fruits = new ArrayList<>();
        fruits.add("Banana");
        fruits.add("Orange");
        fruits.add("Mango");
        fruits.add("Guava");
        FormElement element41 = FormElement.createInstance().setType(FormElement.TYPE_PICKER_SINGLE).setTitle("Single Item").setOptions(fruits);
        FormElement element42 = FormElement.createInstance()
                                           .setType(FormElement.TYPE_PICKER_MULTI_CHECKBOX)
                                           .setTitle("Multi Items")
                                           .setOptions(fruits);
        FormElement element43 = FormElement.createInstance()
                                           .setType(FormElement.TYPE_PICKER_SINGLE)
                                           .setTitle("Single Item With Specified Values")
                                           .setSpecifiedValues(Arrays.<Object>asList(1, 2, 3, 4))
                                           .setOptions(fruits);

        List<String> provinces = Arrays.asList("北京市", "天津市", "上海市", "重庆市", "河北省", "山西省", "辽宁省", "吉林省", "黑龙江省", "江苏省");
        List<String> filterKeys = Arrays.asList("bjs", "tjs", "shs", "cqs", "hbs", "sxs", "lns", "jls", "hljs", "jss");
        FormElement element44 = FormElement.createInstance()
                                           .setType(FormElement.TYPE_PICKER_SINGLE_WITH_FILTER)
                                           .setTitle("Single Item With With Filter")
                                           .setOptions(provinces)
                                           .setFilterKeys(filterKeys)
                ;

        FormHeader header5 = FormHeader.createInstance().setTitle("RelationShip");
        FormElement element51 = FormElement.createInstance().setType(FormElement.TYPE_EMPTY).setTitle("No Data Temp");

        List<FormObject> formItems = new ArrayList<>();
        formItems.add(header1);
        formItems.add(element11);
        formItems.add(element12);
        formItems.add(element13);
        formItems.add(element14);
        formItems.add(header2);
        formItems.add(element21);
        formItems.add(element22);
        formItems.add(element23);
        formItems.add(header3);
        formItems.add(element31);
        formItems.add(element32);
        formItems.add(element33);
        formItems.add(element34);
        formItems.add(header4);
        formItems.add(element41);
        formItems.add(element42);
        formItems.add(element43);
        formItems.add(element44);
        formItems.add(header5);
        formItems.add(element51);

        mFormBuilder.addFormElements(formItems);
        mFormBuilder.refreshView();

    }

    private OnFormElementValueChangedListener valueChangedListener = new OnFormElementValueChangedListener() {
        @Override
        public void onValueChanged(FormElement formElement) {
            if (formElement.getType() == FormElement.TYPE_PICKER_SINGLE) {
                Toast.makeText(FullscreenFormActivity.this,
                               formElement.getTitle() + "修改成为" + formElement.getValue() + ", 指定值为: " + formElement.getSpecifiedValue(),
                               Toast.LENGTH_SHORT).show();
            } else if (formElement.getType() == FormElement.TYPE_PICKER_DATE) {
                Toast.makeText(FullscreenFormActivity.this,
                               "选择了日期" + formElement.getDateInMillis() + ", 解析成日期字符" + DateFormat.format("yyyy-MM-dd", formElement.getDateInMillis()),
                               Toast.LENGTH_SHORT).show();
            } else if (formElement.getType() == FormElement.TYPE_PICKER_TIME) {
                Toast.makeText(FullscreenFormActivity.this,
                               "选择了时间" + formElement.getTimeInMillis() + ", 解析成时间字符" + DateFormat.format("HH:mm", formElement.getTimeInMillis()),
                               Toast.LENGTH_SHORT).show();
            } else if (formElement.getType() == FormElement.TYPE_PICKER_DATE_AND_TIME) {
                Toast.makeText(FullscreenFormActivity.this,
                               "选择了日期时间" + formElement.getDateTimeInMillis() + ", 解析成日期时间字符" + DateFormat.format("yyyy-MM-dd HH:mm",
                                                                                                                 formElement.getDateTimeInMillis()),
                               Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(FullscreenFormActivity.this, formElement.getTitle() + "修改成为" + formElement.getValue(), Toast.LENGTH_SHORT).show();
            }
        }
    };

    private void showInputDialog(String value, final EditText et) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Address");
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        input.setText(value);
        input.setHint("Pleas Input Address");
        builder.setView(input);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String value = input.getText().toString();
                if (!TextUtils.isEmpty(value)) {
                    et.setText(value);
                }
            }
        });
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.show();
    }

}
